//
//  STPopoverTestController.swift
//  iPhone
//
//  Created by Toj on 12/7/23.
//

import UIKit

// MARK: - 懒加载(属性), 变量, 初始化init
class STPopoverTestController: UIViewController {
    
    /** <#NOTE#> */
    private lazy var rightBarButton: UIBarButtonItem = {
        let btn = UIButton()
        let view = UIBarButtonItem.init(customView: btn)
        return view
    }()
    
    /** <#NOTE#> */
    private lazy var leftBottomButton: UIButton = {
        let view = UIButton()
        return view
    }()
    
    /** <#NOTE#> */
    private lazy var rightButtomButton: UIButton = {
        let view = UIButton()
        return view
    }()
    
    /** <#NOTE#> */
    private lazy var leftTopButton: UIButton = {
        let view = UIButton()
        return view
    }()
    
    /** <#NOTE#> */
    private lazy var rightCenterButton: UIButton = {
        let view = UIButton()
        return view
    }()
    
    fileprivate var texts = ["Edit", "Delete", "Report"]
    fileprivate var popover: Popover!
    
    deinit {
        print("-->startimes: \(st_className) func: deinit")
    }
}

// MARK: - 生命周期
extension STPopoverTestController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupMakeInitialize()
        setupMakeAddSubviews()
        setupMakeLayoutSubviews()
    }
}

// MARK: - 点击事件/ 通知事件回调
@objc private extension STPopoverTestController {
    
    func tappedRightBarButton(_ sender: UIBarButtonItem) {
        let startPoint = CGPoint(x: self.view.frame.width - 60, y: 55)
        let aView = UIView(frame: CGRect(x: 0, y: 0, width: self.view.frame.width, height: 180))
        let popover = Popover()
        popover.show(aView, point: startPoint)
    }
    
    func tappedLeftBottomButton(_ sender: UIButton) {
        let width = self.view.frame.width / 4
        let aView = UIView(frame: CGRect(x: 0, y: 0, width: width, height: width))
        let popover = Popover()
        popover.popoverType = .up
        popover.showBlackOverlay = false
        popover.cornerRadius = width / 2
        popover.show(aView, fromView: self.leftBottomButton)
    }
    
    func tappedRightButtomButton(_ sender: UIButton) {
        let tableView = UITableView(frame: CGRect(x: 0, y: 0, width: self.view.frame.width, height: 135))
        tableView.delegate = self
        tableView.dataSource = self
        tableView.isScrollEnabled = false
        self.popover = Popover()
        self.popover.popoverType = .auto
        self.popover.blackOverlayColor = UIColor(white: 0.0, alpha: 0.6)
        self.popover.willShowHandler = {
            print("willShowHandler")
        }
        self.popover.didShowHandler = {
            print("didDismissHandler")
        }
        self.popover.willDismissHandler = {
            print("willDismissHandler")
        }
        self.popover.didDismissHandler = {
            print("didDismissHandler")
        }
        self.popover.show(tableView, fromView: self.rightButtomButton)
    }
    
    func tappedLeftTopButton(_ sender: UIButton) {
        let width = self.view.frame.width / 4
        let aView = UIView(frame: CGRect(x: 0, y: 0, width: width, height: width))
        let popover = Popover()
        popover.showBlackOverlay = false
        popover.popoverType = .right
        popover.show(aView, fromView: self.leftTopButton)
    }
    
    func tappedRightCenterButton(_ sender: UIButton) {
        let width = self.view.frame.width / 4
        let aView = UIView(frame: CGRect(x: 0, y: 0, width: 96, height: 25))
        let popover = Popover()
        popover.cornerRadius = 4
        popover.popoverType = .left
        popover.popoverColor = .red
        popover.showBlackOverlay = false
        popover.arrowSize = CGSize(width: 12, height: 6)
        //popover.backgroundColor = .black
        
        popover.show(aView, fromView: rightCenterButton)
    }
    
    func tappedAPositionButton(_ sender: UIButton) {
        let width = self.view.frame.width / 4
        let aView = UIView(frame: CGRect(x: 0, y: 0, width: width, height: width))
        let popover = Popover()
        popover.popoverType = .auto
        popover.showBlackOverlay = false
        popover.show(aView, fromView: sender)
    }
}

// MARK: - 子类可重写方法/ objc事件
@objc extension STPopoverTestController {
    
}

// MARK: - Public
extension STPopoverTestController {
    
}

// MARK: - Private
private extension STPopoverTestController {
    
}

extension STPopoverTestController: UITableViewDelegate {
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        self.popover.dismiss()
    }
}

extension STPopoverTestController: UITableViewDataSource {
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
        return 3
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = UITableViewCell(style: .default, reuseIdentifier: nil)
        cell.textLabel?.text = self.texts[(indexPath as NSIndexPath).row]
        return cell
    }
}

// MARK: - 初始化设置
private extension STPopoverTestController {
    
    func setupMakeAddSubviews() {
        
    }
    
    func setupMakeLayoutSubviews() {
        
    }
    
    func setupMakeInitialize() {
        
    }
}
